﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace UIT_CV_Management
{
    public class MonThiTotNghiep
    {
        private String id;
        public String maMonThiTotNghiep;
        public String tenMonThiTotNghiep;
        private float ketQuaThi;

        // database
        Database myDatabase = Database.shareDatabase;
        public MonThiTotNghiep(String _tenMonThiTotNghiep,float _ketQuaThi)
        {
            ketQuaThi = _ketQuaThi;
            tenMonThiTotNghiep = _tenMonThiTotNghiep;

            id = this.getIDFromDatabase();
            maMonThiTotNghiep = this.getMaMonThiTotNghiepFromDatabase();
        }
        public MonThiTotNghiep(String _tenMonThiTotNghiep, float _ketQuaThi, int index)
        {
            ketQuaThi = _ketQuaThi;
            tenMonThiTotNghiep = _tenMonThiTotNghiep;

            id = this.getIDFromDatabase();
            this.IDaddIndex(index);

            maMonThiTotNghiep = this.getMaMonThiTotNghiepFromDatabase();
        }
        public MonThiTotNghiep(String _id)
        {
            QueryResult result =  myDatabase.Query("SELECT * FROM MonThiTotNghiep WHERE ID = '" + _id + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                // get info
                id = table.Rows[0]["ID"].ToString();
                maMonThiTotNghiep = table.Rows[0]["MaMonThiTotNghiep"].ToString();
                tenMonThiTotNghiep = table.Rows[0]["TenMonThiTotNghiep"].ToString();
                ketQuaThi = float.Parse(table.Rows[0]["KetQuaThi"].ToString());

            }
        }
        private String getIDFromDatabase()
        {
            String x;
            x = Database.getID("MonThiTotNghiep", "ID");

            return x;
        }
        private String getMaMonThiTotNghiepFromDatabase()
        {
            String x;
            x = Database.getID("MonThiTotNghiep", "MaMonThiTotNghiep");
            return x;
        }
        private void IDaddIndex(int _index)
        {
            
        }
        public void save()
        {
            String sqlMonThiTotNghiep = "INSERT INTO MonThiTotNgiep VALUES('" + id + "','" + maMonThiTotNghiep + "','" + tenMonThiTotNghiep + "'," + ketQuaThi + ")";
            QueryResult result = myDatabase.Query(sqlMonThiTotNghiep);

            if (result.ReturnCode == 409)
            {
                MessageBox.Show("Lỗi khi lưu dữ liệu Nghề Nghiệp vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void update()
        {
            bool isDupplicated = Database.isDupplicate("MonThiTotNghiep", "ID", id);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE MonThiTotNghiep SET TenMonThiTotNghiep = '" + tenMonThiTotNghiep + "', KetQuaThi = " + ketQuaThi 
                    + " WHERE ID = '" + id + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    MessageBox.Show("Lỗi khi update liệu Thông Tin Phụ vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }
        public void delete()
        {
            String sqlDelete = "DELETE FROM MonThiTotNghiep WHERE ID = '" + id + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                MessageBox.Show("Lỗi xoá dữ liệu trong database.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
